package devcamp.team.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.OverlayItem;

import devcamp.team.data.WowoooooooUserData;

public class PinItemOverlay extends ItemizedOverlay<OverlayItem> {

    private List<WowoooooooUserData> list;
    private MapController mapController;
    private Context mContext;
    private Drawable pinMarker;

    private List<OverlayItem> items = new ArrayList<OverlayItem>();


    public PinItemOverlay(Drawable defaultMarker) {
        super(defaultMarker);
    }

    public PinItemOverlay(Drawable defaultMarker, List<WowoooooooUserData> userList) {
        super(defaultMarker);
        pinMarker = defaultMarker;
        list = userList;
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        //        if (list == null) {
        //            return null;
        //        }
        WowoooooooUserData data = list.get(i);
        int lat = (int) (data.getLatitude() * 1E6);
        int lng = (int) (data.getLongitude() * 1E6);
        GeoPoint point = new GeoPoint(lat, lng);
        //OverlayItem overlayItem = new OverlayItem(point, data.getUserName(), "");
        OverlayItem overlayItem = new OverlayItem(point, data.getUserName(), data.getLocationName());
        overlayItem.setMarker(boundCenterBottom(pinMarker));
        return overlayItem;
        //        return items.get(i);
    }

    @Override
    public int size() {
        //        if (list == null) {
        //            return 0;
        //        }
        return list.size();
        //        return items.size();
    }

    @Override
    protected boolean onTap(int index) {	
        //mapController.animateTo(this.getItem(index).getPoint());

    	Log.d("PinItemOverlay", "onTap");
    	GeoPoint point = this.getItem(index).getPoint();
    	// 指定ポイント中央に移動
        mapController.animateTo(point);
        
        // 位置情報をToast表示
        float latitude = (float) (point.getLatitudeE6() / 1E6);
        float longitude = (float) (point.getLongitudeE6() / 1E6);
        String locationName = this.getItem(index).getSnippet();
        WowoooooooUtils.toastLocation(mContext, latitude, longitude, locationName);

        return true;
    }

    public void addPin(GeoPoint point, String title, String snippet) {
        items.add(new OverlayItem(point, title, snippet));
        populate();
    }

    public void setMapController(MapController mapController) {
        this.mapController = mapController;
    }
    
    public void setContext(Context context) {
    	this.mContext = context;
    }

}
